package com.feather.common.text;

import java.util.ArrayList;

/**
 * @author flogyin
 */
public class CharPool {
	private int					length;
	private ArrayList<char[]>	pool	= new ArrayList<char[]>();

	public int getLength() {
		return this.length;
	}

	public void addReaded(char[] chars) {
		if (chars == null) {
			return;
		}
		pool.add(chars);
		length += chars.length;
	}

	public void addReaded(char[] chars, int offset) {
		if (chars == null) {
			return;
		}
		if (offset > chars.length) {
			offset = chars.length;
		}
		if (offset <= 0) {
			return;
		}
		if (offset == chars.length) {
			pool.add(chars);
			length += chars.length;
		} else {
			char[] swap = new char[offset];
			System.arraycopy(chars, 0, swap, 0, offset);
			pool.add(swap);
			length += offset;
		}
	}

	public char[] totalChars() {
		if (length == 0) {
			return null;
		}
		int size = pool.size();
		char[] res = new char[length];
		int start = 0;
		for (int i = 0; i < size; i++) {
			char[] temp = (char[]) pool.get(i);
			System.arraycopy(temp, 0, res, start, temp.length);
			start += temp.length;
		}
		return res;
	}
}
